// miniprogram/pages/Register/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    matchinfo:undefined,
    baseimgaeUrl: app.globalData.imageBaseUrl,
    timer:'10分钟',
    playername:'',
    reguserinfo:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //var matchinfoobj = JSON.parse(options.matchinfo.replace(" ", "").replace(/\ufeff/g, ""));
    console.log(options);
    var jsonmatchinfoobj = JSON.parse(options.matchinfo);
    jsonmatchinfoobj.matchisreg= 0;
    
    this.setData({
      matchinfo:jsonmatchinfoobj,
      playername: options.playername,
      reguserinfo : app.globalData.reguserinfo
      });
    
    wx.setNavigationBarTitle({
      title:  options.playername + "报名成功支付页面-"+ jsonmatchinfoobj.matchname ,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  onResize:function(res) {
    this.onShow();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  

  wechatpay:function(){
      this.generateOrder(app.globalData.openid);
  },

  generateRemark:function(){
      var remarkobj = new Object();
      remarkobj.phoneno = this.data.reguserinfo.phoneno;
      remarkobj.mail = this.data.reguserinfo.mail;
      remarkobj.name = this.data.reguserinfo.name;
      remarkobj.matchname = this.data.matchinfo.matchname;
      remarkobj.regfee=this.data.matchinfo.regfee;
      console.log(remarkobj);
    
      var  remarkobjstr = JSON.stringify(remarkobj);

      console.log(remarkobjstr);
      return remarkobjstr;
  },

  /**生成商户订单 */
  generateOrder: function (openid) {
    var that = this
   console.log(app.globalData.appId);
   
    //统一支付
    wx.request({
      url: app.globalData.interfaceWehchatpayURL,
      method: 'GET',
      data: {
        total_fee: that.data.matchinfo.regfee * 100,
        body: that.data.playername + "支付" +that.data.matchinfo.matchname+ "报名费" +  that.data.matchinfo.regfee + "元" ,
        attach: this.generateRemark(),
        openid: openid,
        appid:app.globalData.appId
      },
      success: function (res) {
        console.log(res) ;
        if(res.data.RETURN_CODE!= undefined && res.data.RETURN_CODE=="FAIL"){
         wx.showToast({
           title: res.data.RETURN_MSG,
           icon:'none'
         })
          return ;
        }
        
        var pay = res.data;          //发起支付
        var timeStamp = pay.timeStamp;
        console.log("timeStamp:" + timeStamp)                
        var packages = pay.package;
        console.log("package:" + packages)                
        var paySign = pay.paySign;
        console.log("paySign:" + paySign)               
        var nonceStr = pay.nonceStr;
        console.log("nonceStr:" + nonceStr)                
        var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr };
        that.pay(param)
      },
    })
  },   
  
   /* 支付   */
  pay: function (param) {
    console.log("支付")
    console.log(param)
    wx.requestPayment({
      timeStamp: param.timeStamp,
      nonceStr: param.nonceStr,
      package: param.package,
      signType: param.signType,
      paySign: param.paySign,
      success: function (res) {
        // success
        console.log("支付")
        console.log(res)
        wx.navigateBack({
          delta: 1, // 回退前 delta(默认为1) 页面
          success: function (res) {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function () {
            // fail

          },
          complete: function () {
            // complete
          }
        })
      },
      fail: function (res) {
        // fail
        console.log("支付失败")
        console.log(res)
      },
      complete: function () {
        // complete
        console.log("pay complete")
      }
    })
  },
})